﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
using System.Threading;
using SPA_Prototype;


namespace SPA_Prototype.Model
{
    public class Email
    {
        private static string _server = "smtp.gmail.com";   // SMTP Server
        private static string _user = "spamanagement.mail@gmail.com";          // user's name of this server
        private static string _password = "spamanagement.mail";           // password
        public static string Receiver = "spamanagement.mail@gmail.com";      // receiver's email
        private System.Net.Mail.Attachment _attachment;

        private MailMessage mail;// = new MailMessage();
        private SmtpClient SmtpServer;//= new SmtpClient(_server);

        public string Status = "";
        public Email(string Subject, string Body, string AttachFilePath)
        {
            // init the server
            SmtpInit();

            // add subject and body to the email
            mail.Subject = Subject;
            mail.Body = Body;

            // attach file
            try
            {
                _attachment = new System.Net.Mail.Attachment(AttachFilePath);
                mail.Attachments.Add(_attachment);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message); // if the file's path is wrong
            }

            // send it
            new Thread(new ThreadStart(SendEmail)).Start();

        }
        public Email(string receiver, string AttachFilePath, int i)
        {
            // init the server
            Receiver = receiver;
            SmtpInit();

            // add subject and body to the email  
            mail.Subject = "DATA BACKUP " + DateTime.Now.Month + "/" + DateTime.Now.Day + "/" + DateTime.Now.Year;
            mail.Body = "";

            // attach file
            try
            {
                _attachment = new System.Net.Mail.Attachment(AttachFilePath);
                mail.Attachments.Add(_attachment);
            }
            catch (Exception ex)
            {
              //  if(ex.Message != "Illegal characters in path.")
                MessageBox.Show(ex.Message); // if the file's path is wrong
            }

            // send it
            Thread t = new Thread(new ThreadStart(SendEmail));
            t.Start();


        }
        public Email(string Subject, string Body)
        {
            // init the server
            SmtpInit();

            // add subject and body to the email
            mail.Subject = Subject;
            mail.Body = Body;

            // send it
            new Thread(new ThreadStart(SendEmail)).Start();
        }
        public Email(string Body)
        {
            // init the server
            SmtpInit();

            // add subject and body to the email
            mail.Subject = "SPA BILL   " + DateTime.Now.ToString();
            mail.Body = Body;

            // send it
            Thread t = new Thread(new ThreadStart(SendEmail));
            t.Start();
 
        }
        private void SmtpInit()
        {
            mail = new MailMessage();
            SmtpServer = new SmtpClient(_server);

            // init sender's mail and receiver's mail
            mail.From = new MailAddress(_user);
            mail.To.Add(Receiver);

            // init some informations to interact with SMTP Server
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential(_user, _password);
            SmtpServer.EnableSsl = true;
        }
        private void SendEmail()
        {
            try
            {
                SmtpServer.Send(mail);
            }
            catch (Exception ex)
            {
                Status = "Failure to sending email";
                // MessageBox.Show(ex.Message); // if something wrong occur, maybe errors form network
                SPA_Prototype.Program.f1.SendingEmailStatus(Status);
                return;
            }
            Status = "Sending email successful";
            SPA_Prototype.Program.f1.SendingEmailStatus(Status);
        }

   
        
    }

    public class EmailManagement
    {
        private DatabaseDataSet.EmailAdressDataTable _DataTable;
        private DatabaseDataSetTableAdapters.EmailAdressTableAdapter _Adt;

        // constructor
        public EmailManagement()
        {
            _DataTable = new DatabaseDataSet.EmailAdressDataTable();
            _Adt = new DatabaseDataSetTableAdapters.EmailAdressTableAdapter();

            LoadDB();

        }

        // property/field
        private string _EmmaiAddress = "";
        public string EmailAdress
        {
            get { return _EmmaiAddress; }
            set
            {
                _EmmaiAddress = value;
                if (_DataTable.Rows.Count <= 0) _DataTable.AddEmailAdressRow(_EmmaiAddress);
                else _DataTable[0].Email = _EmmaiAddress;
                UpdateDB();
            }
        }

        //method
        public string SendMailWithNewBill(BillTemplate NewBill)
        {
        //    if (_DataTable.Rows.Count > 0)
          //  {
                Email.Receiver = _DataTable[0].Email;
                Email eml = new Email(NewBill.Template);
            //} 
                return "Sending Email..";
        }

        private void LoadDB()
        {
            _Adt.Fill(_DataTable);
            if (_DataTable.Rows.Count > 0) _EmmaiAddress = _DataTable[0].Email;
        }
        private void UpdateDB()
        {
            try
            {
                _Adt.Update(_DataTable);
                MessageBox.Show("Successful !", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

    public class BillTemplate
    {

        private List<InvoiceData> _DataList;
        public BillTemplate(List<InvoiceData> dt, string BillNo,string Cashier)
        {

            _DataList = dt;

            Template = "------------------------------------------------------------------\r\n"
                        + "------------------------------------------------------------------\r\n"
                        + "-----          GUEST BILL                                -----\r\n"
                        + "------------------------------------------------------------------\r\n"
                        + "------------------------------------------------------------------\r\n"
                        + "  Customer's name : " + _DataList[4].Data + "\r\n"
                        + "  Date      : " + _DataList[0].Data + "\r\n"
                        + "  Bill No.  : " + BillNo + "\r\n"
                        + "  Cashier   : " + Cashier + "\r\n"
                        + "  Staff No. : " + _DataList[2].Data + "\r\n"
                        + "  Room No.  : " + _DataList[3].Data + "\r\n"
                        + "------------------------------------------------------------------\r\n"
                        + "|| No | Description | Code | Q'Ty | Unit Price |   VND   | Discount | Notes\r\n\r\n";



            for (int j = 5; j <= 58; )
                for (int i = 0; i < 6; i++)
                {
                    for (int k = 0; k < 9; k++)
                    {
                        string s = "   " + _DataList[j++].Data;
                        if (s.Length < 25)
                            for (int t = 0; t < (25 - s.Length); t++) s += " ";
                        Template += s;
                    }
                    Template += "\r\n";
                }
            Template += "\r\n\r\n";
            Template += "  Raw Total     : " + _DataList[59].Data + "\r\n"
                       + "  Total Discount: " + _DataList[61].Data + "\r\n"
                       + "  Voucher       : " + _DataList[64].Data + "\r\n"
                       + "  " + _DataList[65].Data + "\r\n"
                       + "  Total         : " + _DataList[66].Data + "\r\n"
                       + "  Equivalent    : " + _DataList[67].Data + "            Exchange Rate : " + _DataList[63].Data + "\r\n";

        }

        public string Template;
    }
}
